package correct.day6;

public class Test3 {
    public static void main(String[] args) {
        System.out.println(solve("11", "99"));
    }

    public static String solve (String s, String t) {
        // write code here
        char[] arr1 = new StringBuffer(s).reverse().toString().toCharArray();
        char[] arr2 = new StringBuffer(t).reverse().toString().toCharArray();

        int m = arr1.length;
        int n = arr2.length;
        int[] tmp = new int[m+n];

        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                tmp[i+j] += (arr1[i]-'0') * (arr2[j]-'0');
            }
        }

        int a = 0;
        for(int i = 0; i < m+n; i++) {
            if(a > 0) {
                tmp[i] += a;
                a = 0;
            }
            if(tmp[i] > 9) {
                a = tmp[i] / 10;
                tmp[i] %= 10;
            }
        }

        for(int i = m+n-1; i >= 0; i--) {
            if (tmp[i] != 0) {
                break;
            }
            if (m+n == 2 && i == 0) {
                break;
            }
            tmp[i] = Integer.MAX_VALUE;
        }

        StringBuffer stringBuffer = new StringBuffer();
        for(int i = 0; i < m+n; i++) {
            stringBuffer.append(tmp[i]);
        }
        stringBuffer.deleteCharAt(stringBuffer.length()-1);

        return stringBuffer.reverse().toString();
    }
}
